import 'package:flutter_wan_android/entity/article_entity.dart';
import 'package:flutter_wan_android/entity/home_banner_entity.dart';
import 'package:flutter_wan_android/provider/view_state_model_list_refresh.dart';
import 'package:flutter_wan_android/service/wan_android_repository.dart';

class HomeModel extends ViewStateRefreshListModel {
  List<HomeBannerEntity> _banners;
  List<ArticleEntity> _topArticles;

  List<HomeBannerEntity> get banners => _banners;

  List<ArticleEntity> get topArticles => _topArticles;


  @override
  Future<List> loadData({int pageNum}) async {
    List<Future> futures = [];

    ///如果当前是刷新时，获取banner与置顶文章
    if (pageNum == ViewStateRefreshListModel.pageNumFirst) {
      futures.add(WanAndroidRepository.fetchBanners());
      futures.add(WanAndroidRepository.fetchTopArticles());
    }
    futures.add(WanAndroidRepository.fetchArticles(pageNum));

    var result = await Future.wait(futures);
    if (pageNum == ViewStateRefreshListModel.pageNumFirst) {
      _banners = result[0];
      _topArticles = result[1];
      return result[2];
    } else {
      return result[0];
    }
  }

  @override
  onCompleted(List data) {}
}
